package com.ugrow.ugrowcommunity.activity.announcement;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.announcement.vm.ActivitysFragment;
import com.ugrow.ugrowcommunity.activity.announcement.vm.EducationFragment;
import com.ugrow.ugrowcommunity.activity.announcement.vm.LogisticsFragment;



/**
 * @author qingsu
 * 新闻首页
 */
public class AnnouncementActivity extends AppCompatActivity {
    private ActivitysFragment activitysFragment;
    private EducationFragment educationFragment;
    private LogisticsFragment logisticsFragment;
    private RadioGroup mRadioGroup;
    private RadioButton mBtEducation,mBtActivitys,mBtLogistics;
    private ImageView imgBack;


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setStatusBarColor(0xFFFFFF);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_announcement);

        initView();
        cheakRadio();
        initFragment();
        addFragment(activitysFragment,educationFragment,logisticsFragment);
        hideFragment(activitysFragment,logisticsFragment);
        showFragment(educationFragment);
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startActivity(new Intent(AnnouncementActivity.this, MainActivity.class));
                finish();
            }
        });
    }


    //初始化空间地址
    private void initView(){
        mRadioGroup = findViewById(R.id.grop);
        imgBack = findViewById(R.id.ACback);

    }

    //初始化Fragment
    private void initFragment(){
        activitysFragment = new ActivitysFragment();
        educationFragment = new EducationFragment();
        logisticsFragment = new LogisticsFragment();
    }

    //单选按钮监听
    private void cheakRadio(){
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.bt_education:
                        hideFragment(activitysFragment,logisticsFragment);
                        showFragment(educationFragment);
                        break;
                    case R.id.bt_activitys:
                        hideFragment(educationFragment,logisticsFragment);
                        showFragment(activitysFragment);
                        break;
                    case R.id.bt_logistics:
                        hideFragment(activitysFragment,educationFragment);
                        showFragment(logisticsFragment);
                        break;
                }

            }
        });
    }


    //一次性添加全部Fragment
    private void addFragment(Fragment... fragments){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for(Fragment fragment : fragments){
            transaction.add(R.id.fram,fragment);
        }
        //提交事务
        transaction.commit();
    }

    //设置隐藏
    private  void hideFragment(Fragment... fragments){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for(Fragment fragment : fragments){
            transaction.hide(fragment);
        }
        transaction.commit();
    }

    //设置显示
    private void showFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.show(fragment);
        transaction.commit();
    }




}